import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

let store = new Vuex.Store({
    state: {
        carPanelData: [],
        receiveInfo: [{
            "name": "王某某",
            "phone": "13811111111",
            "areaCode": "010",
            "landLine": "64627856",
            "provinceId": 110000,
            "province": "北京市",
            "cityId": 110100,
            "city": "市辖区",
            "countyId": 110106,
            "county": "海淀区",
            "add": "上地十街辉煌国际西6号楼319室",
            "default": true
        }, {
            "name": "李某某",
            "phone": "13811111111",
            "areaCode": "010",
            "landLine": "64627856",
            "provinceId": 110000,
            "province": "北京市",
            "cityId": 110100,
            "city": "市辖区",
            "countyId": 110106,
            "county": "海淀区",
            "add": "上地十街辉煌国际东6号楼350室",
            "default": false
        }],
        maxOff: false,
        carShow: false,
        carTimer: null,
        orderData: []
    },
    getters: {
        //计算购物车内商品总数
        totleCount(state) {
            let count = 0
            state.carPanelData.forEach((goods) => {
                count += goods.count
            })
            return count
        },
        //计算购物车内总商品价格
        totlePrice(state) {
            let price = 0
            state.carPanelData.forEach((goods) => {
                price += goods.price * goods.count
            })
            return price
        },
        //全选按钮变化
        allChecked(state) {
            let allChecked = true
            state.carPanelData.forEach((goods) => {
                if (!goods.checked) {
                    allChecked = false
                    return
                }
            })
            return allChecked
        },
        //购物车详情页已选总数
        checkedCount(state) {
            let count = 0
            state.carPanelData.forEach((goods) => {
                if (goods.checked) {
                    count += goods.count
                }
            })
            return count
        },
        //应付金额计算
        checkedPrice(state) {
            let price = 0
            state.carPanelData.forEach((goods) => {
                if (goods.checked) {
                    price += goods.count * goods.price
                }
            })
            return price
        },
        //选中的商品
        checkedGoods(state) {
            let checkedGoods = []
            state.carPanelData.forEach((goods) => {
                if (goods.checked) {
                    checkedGoods.push(goods)
                }
            })
            return checkedGoods
        }
    },
    mutations: {
        //将商品加入购物车中
        addCarPanelData(state, data) {
            let bOff = true
            state.carPanelData.forEach((goods) => {
                if (goods.sku_id === data[0].sku_id) {
                    goods.count += data[1]
                    bOff = false
                    if (goods.count > goods.limit_num) {
                        goods.count -= data[1]
                        state.maxOff = true
                        return
                    }
                }
                state.carShow = true
            })
            if (bOff) {
                let goodsData = data[0]
                Vue.set(goodsData, 'count', data[1])
                Vue.set(goodsData, 'checked', true)
                state.carPanelData.push(goodsData)
                state.carShow = true
            }
            console.log(state.carPanelData)
        },
        //购物车详情页加
        plusCarPanelData(state, id) {
            state.carPanelData.forEach((goods, index) => {
                if (goods.sku_id === id) {
                    if (goods.count === goods.limit_num) return
                    goods.count++
                    return
                }
            })
        },
        //购物车详情页减
        subCarPanelData(state, id) {
            state.carPanelData.forEach((goods, index) => {
                if (goods.sku_id === id) {
                    if (goods.count === 1) return
                    goods.count--
                    return
                }
            })
        },
        //单项勾选
        checkGoods(state, id) {
            state.carPanelData.forEach((goods, index) => {
                if (goods.sku_id === id) {
                    goods.checked = !goods.checked
                }
            })
        },
        //全选按钮控制
        allGoodsCheck(state, checked) {
            if (checked) {
                state.carPanelData.forEach((goods, index) => {
                    goods.checked = false
                })
            } else {
                state.carPanelData.forEach((goods, index) => {
                    goods.checked = true
                })
            }
        },
        //购物车页删除选中商品
        delCheckGoods(state) {
            let i = state.carPanelData.length
            while (i--) {
                if (state.carPanelData[i].checked) {
                    state.carPanelData.splice(i, 1)
                }
            }
            // state.carPanelData.forEach((goods, index) => {
            //     if (goods.checked) {
            //         state.carPanelData.splice(index, 1)
            //     }
            // })
        },
        //购物车中删除商品
        delCarPanelData(state, id) {
            state.carPanelData.forEach((goods, index) => {
                if (goods.sku_id === id) {
                    state.carPanelData.splice(index, 1)
                    return
                }
            })
        },
        alertPrompt(state) {
            state.maxOff = true
        },
        //关闭提示窗
        closePrompt(state) {
            state.maxOff = false
        },
        //鼠标触碰显示和隐藏购物车
        showCar(state) {
            clearTimeout(state.carTimer)
            state.carShow = true
        },
        hideCar(state) {
            state.carTimer = setTimeout(() => {
                state.carShow = false
            }, 500)
        },
        submitReceive(state, data) {
            if (data.default) {
                state.receiveInfo.forEach((receive) => {
                    receive.default = false
                })
            }
            state.receiveInfo.push(data)
        },
        submitOrder(state, data) {
            state.orderData.unshift(data)
            //拿掉购物车商品
            let i = state.carPanelData.length
            while (i--) {
                if (state.carPanelData[i].checked) {
                    state.carPanelData.splice(i, 1)
                }
            }
        },
        payNow(state, id) {
            state.orderData.forEach((order) => {
                if (order.orderId === id) {
                    order.isPay = true
                    return
                }
            })
        },
        checkDefault(state, data) {
            state.receiveInfo.forEach((receive, index) => {
                if (receive == data) {
                    receive.default = true
                    //        state.receiveInfo.unshift(state.receiveInfo.splice(index,1)[0])
                } else {
                    receive.default = false
                }
            })
        },
        // checkOut(state, data) {
        //     state.provisionalOrder = data
        // }
    }
})

export default store